package com.undsf.domainganker.chinaz;

import com.undsf.utils.PairList;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Arathi on 2015/4/25.
 */
public class SearchConfig {
    //关键字位置 0: 任意 1: 左 2: 右 3: 左或右
    public static final int POSITION_ANY = 0;
    public static final int POSITION_START = 1;
    public static final int POSITION_END = 2;
    public static final int POSITION_START_OR_END = 3;

    public static final int LENGTH_NULL = -1;

    //组成（有多组） 1: 字母 2: 数字 3: 横杆
    public static final int COMPOSITIONS_ALPHABET = 1;
    public static final int COMPOSITIONS_DIGIT = 2;
    public static final int COMPOSITIONS_LINKTO = 3;

    //特殊结构

    //排序方式 1: 长度 2: 字母 3: 后缀 4: 日期-字母 5: 日期-长度
    public static final int SORT_LENGTH = 1;
    public static final int SORT_ALPHABET = 2;
    public static final int SORT_SUFFIX = 3;
    public static final int SORT_DATE_ALPHABET = 4;
    public static final int SORT_DATE_LENGTH = 5;

    //后缀 目前只支持4种 com/net/org/cn
    public static final String SUFFIX_COM = "com";
    public static final String SUFFIX_NET = "net";
    public static final String SUFFIX_ORG = "org";
    public static final String SUFFIX_CN = "cn";

    //过期类型 0: 全部 1: 已删除 2: 即将删除
    public static final int STATUS_BOTH = 0;
    public static final int STATUS_DELETED = 1;
    public static final int STATUS_PRE_RELEASE = 2;

    //过期时间 0: 即将删除 1: 已删除(默认) yyyy-MM-dd: 具体删除日期
    public static final String DATE_PRE_RELEASE = "0";
    public static final String DATE_DELETED = "1";

    //每页数量（没有实际意义，都是30）
    public static final int PAGE_SIZE_30 = 30;
    public static final int PAGE_SIZE_50 = 50;
    public static final int PAGE_SIZE_100 = 100;
    public static final int PAGE_SIZE_150 = 150;
    public static final int PAGE_SIZE_200 = 200;

    public static final int ST_DEFAULT = 1;

    public String keyWord; //kw 包含关键字
    public int position; //p 关键字位置
    public int beginLength; //bl 开始长度 最小长度
    public int endLength; //el 结束长度 最大长度
    public List<Integer> compositions; //ds[] ds%5B%5D
    //public int structure; //pl 特殊结构
    public int sort; //sort 排序方式
    public List<String> suffixs; //suffix[] suffix%5B%5D
    public int deleteType; //dt 删除类型
    public String deleteDate; //date 日期
    public int pageSize; //pagesize 每页数量（没有实际效果）
    public int st; //st （作用不明）
    public int page; //page 页号

    public SearchConfig(){
        //设置默认值
        keyWord = "";
        position = POSITION_ANY;
        beginLength = endLength = LENGTH_NULL;
        compositions = new ArrayList<Integer>();
        compositions.add(COMPOSITIONS_ALPHABET);
        sort = SORT_LENGTH;
        suffixs = new ArrayList<String>();
        suffixs.add(SUFFIX_COM);
        deleteType = STATUS_BOTH;
        deleteDate = DATE_DELETED;
        pageSize = PAGE_SIZE_30;
        st = ST_DEFAULT;
        page = 1;
    }

    public void setLength(int begin, int end){
        beginLength = begin;
        endLength = end;
    }

    public void addComposition(int composition){
        if (!compositions.contains(composition)){
            compositions.add(composition);
        }
    }

    public void addSuffix(String suffix){
        if (!suffixs.contains(suffix)){
            suffixs.add(suffix);
        }
    }

    public void setKeyWord(String keyWord) {
        this.keyWord = keyWord;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public void setSort(int sort) {
        this.sort = sort;
    }

    public void setDeleteType(int deleteType) {
        this.deleteType = deleteType;
    }

    public void setDeleteDate(String deleteDate) {
        this.deleteDate = deleteDate;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public void nextPage(){
        page++;
    }

    private String int2str(int i){
        if (i<0) return "";
        return i+"";
    }

    public PairList<String,String> toPairList(){
        PairList<String,String> list = new PairList<String,String>();
        list.add("kw", keyWord);
        list.add("p", int2str(position));
        list.add("bl", int2str(beginLength));
        list.add("el", int2str(endLength));
        for (int ds : compositions){
            list.add("ds%5B%5D", Integer.toString(ds));
        }
        //list.add("pl", Integer.toString(structure));
        list.add("sort", Integer.toString(sort));
        for (String suffix : suffixs){
            list.add("suffix%5B%5D", suffix);
        }
        list.add("dt", Integer.toString(deleteType));
        list.add("date", deleteDate); //Integer.toString()
        list.add("pagesize", Integer.toString(pageSize));
        list.add("st", Integer.toString(st));
        list.add("page", Integer.toString(page));
        return list;
    }

    @Override
    public String toString(){
        String params = null;
        params = "kw=" + keyWord;
        params += "&" + "p=" + position;
        params += "&" + "bl=" + beginLength;
        params += "&" + "el=" + endLength;
        for (int ds : compositions){
            params += "&" + "ds%5B%5D=" + ds;
        }
        //params += "&" + "pl=" + structure;
        params += "&" + "sort=" + sort;
        for (String suffix : suffixs){
            //list.add("ds%5B%5D", suffix);
            params += "&" + "suffix%5B%5D=" + suffix;
        }
        params += "&" + "dt=" + deleteType;
        params += "&" + "date=" + deleteDate;
        params += "&" + "pagesize=" + pageSize;
        params += "&" + "st=" + st;
        params += "&" + "page=" + page;
        return params;
    }
}
